<template>
  <div class="option-form">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">
      <el-form-item :label="activeType ? activeType.name : '' " prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">
          {{activeItem ? '编辑':'立即创建'}}
        </el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import OptionsApi from '@/api/options';

export default {
  name: 'OptionForm',
  props: ['activeType', 'activeItem'],
  data() {
    return {
      baseUrl: 'http://backserve.com:3000',
      ruleForm: {
        name: this.activeItem ? this.activeItem.name : '',
      },
      rules: {
        name: [
          {
            required: true,
            message: '请输入名称',
            trigger: 'blur',
          },
          {
            min: 2,
            max: 10,
            message: '长度在2到10个字符',
            trigger: 'blur',
          },
        ],
      },
    };
  },
  mounted() { },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.userItem) {
            // UserApi.putUser(this.ruleForm)
            //   .then((data) => {
            //     if (data.data) {
            //       this.$message({
            //         message: data.message,
            //         type: 'success',
            //       });
            //       this.resetForm('ruleForm');
            //       this.$emit('update:dialogVisible', false);
            //     } else {
            //       this.resetForm('ruleForm');
            //       this.$emit('update:dialogVisible', false);
            //       this.$message({
            //         message: `${data.message}`,
            //         type: 'error',
            //       });
            //     }
            //   })
            //   .catch((err) => {
            //     this.$message({
            //       message: `${err.message}`,
            //       type: 'error',
            //     });
            //   });
          } else {
            this.ruleForm.label = this.activeType.label;
            this.ruleForm.type = this.activeType.type;
            OptionsApi.createOptions(this.ruleForm)
              .then((data) => {
                if (data.data) {
                  this.$message({
                    message: data.message,
                    type: 'success',
                  });
                  this.resetForm('ruleForm');
                  this.$emit('update:dialogVisible', false);
                } else {
                  this.$message({
                    message: data.message,
                    type: 'error',
                  });
                }
              })
              .catch((err) => {
                this.$message({
                  message: err.message,
                  type: 'error',
                });
              });
          }
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  watch: {},
};
</script>

<style lang="scss" scoped>
.option-form {
  width: 100%;
  height: auto;
}
</style>
